import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/User/Users.vue'

Vue.use(VueRouter)

const routes = [
  {
    //把login设置默认首页
    path: '/',
    //重定向
    redirect: '/login'

  },
  {
    //路由地址
    path: '/login',
    component: Login
  },
  {
    //路由地址
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children:[
      {
      path: '/welcome',
      component: Welcome
      },
      {
        path: '/users',
        component: Users
      }
  
  
  ]
  },
]

const router = new VueRouter({
  routes
})

//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  //to 将要访问的路径
  //from 从那个路径跳转过来
  //next 示放行
  if (to.path == '/login') return next();
  // 获取token
  const tokenstr = window.sessionStorage.getItem('token');
  //如果没有token 回到登入
  if (!tokenstr) return next('/login');
  //放行
  next();

})



export default router
